cask "devonthink" do
  on_monterey :or_older do
    on_catalina :or_older do
      version "3.9.6"
      sha256 "e272af94a61619adaf729de336e1ef24465a5e6ff27ed6ae8cb11d28ca35638a"
    end
    on_big_sur :or_newer do
      version "3.9.16"
      sha256 "a2f29f479900cd7fc56bd574d87a64f22089ab9b5cbc8cdeb1cebf33b9220fae"
    end

    url "https://download.devontechnologies.com/download/devonthink/#{version}/DEVONthink_#{version.major}.app.zip"

    livecheck do
      skip "Legacy version"
    end

    app "DEVONthink #{version.major}.app"
  end
  on_ventura :or_newer do
    version "4.1.1"
    sha256 "a2f29f479900cd7fc56bd574d87a64f22089ab9b5cbc8cdeb1cebf33b9220fae"

    url "https://download.devontechnologies.com/download/devonthink/#{version}/DEVONthink.app.zip"

    # The appcast may include unstable versions where upstream doesn't specify a
    # separate channel, so we have to identify stable versions using a regex.
    livecheck do
      url "https://api.devontechnologies.com/1/apps/sparkle/sparkle.php?id=300900000"
      regex(/^v?(\d+(?:\.\d+)+)$/i)
      strategy :sparkle do |items, regex|
        items.map { |item| item.version[regex, 1] }
      end
    end

    app "DEVONthink.app"
  end

  name "DEVONthink"
  desc "Collect, organise, edit and annotate documents"
  homepage "https://www.devontechnologies.com/apps/devonthink"

  auto_updates true

  zap trash: [
    "~/Library/Application Scripts/com.devon-technologies.*",
    "~/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/com.devon-technologies.think*.sfl*",
    "~/Library/Application Support/DEVONthink*",
    "~/Library/Caches/com.apple.helpd/Generated/com.devontechnologies.devonthink.help*",
    "~/Library/Caches/com.devon-technologies.think*",
    "~/Library/Containers/com.devon-technologies.*",
    "~/Library/Cookies/com.devon-technologies.think*.binarycookies",
    "~/Library/Group Containers/679S2QUWR8.think*",
    "~/Library/Metadata/com.devon-technologies.think*",
    "~/Library/Preferences/com.devon-technologies.think*",
    "~/Library/Saved Application State/com.devon-technologies.think*.savedState",
    "~/Library/Scripts/Applications/DEVONagent",
    "~/Library/Scripts/Folder Action Scripts/DEVONthink*",
    "~/Library/WebKit/com.devon-technologies.think*",
  ]
end
